package com.bbz.crawler.core.page.dataprocess;

import java.util.ArrayList;
import java.util.List;

import com.bbz.crawler.core.commen.model.CrawlerResult;
import com.bbz.crawler.core.commen.model.CrawlerResultDetail;
import com.bbz.crawler.core.page.model.GithubJavaModel;

import us.codecraft.webmagic.Page;

public class GithubJavaDataProcess {

	public GithubJavaModel dataProcess(Page page) {

		// 构建CrawlerResult 对象
		CrawlerResult crawlerResult = new CrawlerResult();
		crawlerResult.setTopic("GitHubJAVAProject");
		crawlerResult.setElementName("ProjectName");
		crawlerResult.setElementClass("//ul[@class='repo-list']/div/div/h3/a");
		// 构建CrawlerResultDetail 对象
		List<String> keylist = page.getHtml().xpath("//ul[@class='repo-list']/div/div/h3/a/text()").all();
		List<String> valuelist = page.getHtml().xpath("//ul[@class='repo-list']/div/div/h3/a/@href").all();
		List<CrawlerResultDetail> listResultDetail = new ArrayList<>();
		for (int i = 0; i < keylist.size(); i++) {
			CrawlerResultDetail crawlerResultDetail = new CrawlerResultDetail();
			crawlerResultDetail.setKey(keylist.get(i).toString());
			crawlerResultDetail.setValue(valuelist.get(i).toString());
			listResultDetail.add(crawlerResultDetail);
		}

		// 构建GithubJavaModel
		GithubJavaModel githubJavaModel = new GithubJavaModel();
		githubJavaModel.setCrawlerResult(crawlerResult);
		githubJavaModel.setCrawlerResultDetail(listResultDetail);
		return githubJavaModel;
	}

}
